package com.tanhua.dubbo.api;


import com.tanhua.model.mongo.TestQuestion;
import com.tanhua.model.vo.PageResult;
import org.apache.dubbo.config.annotation.DubboService;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;


import java.util.HashMap;
import java.util.List;
import java.util.Map;
@DubboService
public class TestQuestionApiImpl implements TestQuestionApi {
    @Autowired
    private MongoTemplate mongoTemplate;
    @Override
    public Map<String, TestQuestion> findByIds(List<String> questionId) {
        Map<String, TestQuestion> map=new HashMap<>();
        for (String id : questionId) {
            TestQuestion question = mongoTemplate.findById(new ObjectId(id), TestQuestion.class);
            map.put(id,question);
        }
        return map;
    }

    @Override
    public List<TestQuestion> findAll() {
        List<TestQuestion> list = mongoTemplate.findAll(TestQuestion.class);
        return list;
    }

    @Override
    public PageResult findByPage(Integer page, Integer pageSize) {
        Query query=new Query();
        long count = mongoTemplate.count(query, TestQuestion.class);
        query.skip((page-1)*pageSize).limit(pageSize);
        List<TestQuestion> list = mongoTemplate.find(query, TestQuestion.class);
        return new PageResult(page,pageSize,count,list);

    }

    @Override
    public void deleteById(String id) {
        Criteria criteria = Criteria.where("id").is(new ObjectId(id));
        Query query = Query.query(criteria);
        mongoTemplate.remove(query,TestQuestion.class);
    }

    @Override
    public TestQuestion findById(String id) {
        Criteria criteria = Criteria.where("id").is(new ObjectId(id));
        Query query = Query.query(criteria);

        return mongoTemplate.findOne(query,TestQuestion.class);
    }

    @Override
    public void save(TestQuestion questions) {
        mongoTemplate.save(questions);
    }

    @Override
    public void update(TestQuestion questions,String questionId) {
        Criteria criteria = Criteria.where("id").is(new ObjectId(questionId));
        Query query = Query.query(criteria);
        Update update=new Update();
        update.set("paperId",questions.getPaperId());
        update.set("question",questions.getQuestion());
        mongoTemplate.updateFirst(query,update,TestQuestion.class);
    }

    @Override
    public List<TestQuestion> findByPaperId(String id) {
        Criteria criteria = Criteria.where("paperId").is(id);
        Query query = Query.query(criteria);
        List<TestQuestion> list = mongoTemplate.find(query, TestQuestion.class);
        return list;
    }

    @Override
    public void updateByPaperId(String id) {
        Criteria criteria = Criteria.where("paperId").is(id);
        Query query = Query.query(criteria);
        Update update=new Update();
        update.set("paperId",null);
        mongoTemplate.updateMulti(query,update,TestQuestion.class);
    }
}